package personnal.blogprinter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import personnal.blogprinter.data.PostError;
import personnal.blogprinter.data.PostInfo;
import personnal.blogprinter.data.PostRange;
import personnal.blogprinter.data.Settings;
import personnal.blogprinter.exceptions.ExtractionException;
import personnal.blogprinter.exceptions.NetworkException;

public abstract class BlogPrinter {

	private static Logger logger = Logger.getLogger(BlogPrinter.class);
	
	protected Settings settings;

	protected String fromMonth;

	protected String toMonth;

	public BlogPrinter(Settings settings) throws ExtractionException {
		super();
		this.settings = settings;
		this.fromMonth = settings.getFromMonth();
		this.toMonth = settings.getToMonth();
	}

	public abstract void extract() throws NetworkException, ExtractionException;

	protected void fillInQueue(Collection<PostRange> inputQueue) throws NetworkException, ExtractionException {
		DateTimeFormatter dateParser = DateTimeFormat.forPattern("yyyy-MM");
		DateTime currentMonth = dateParser.parseDateTime(fromMonth);
		DateTime endMonthDate = dateParser.parseDateTime(toMonth);
		if (endMonthDate.isBefore(currentMonth)) {
			throw new IllegalArgumentException("End month date must not be prior to start month date");
		}
		WordpressClient client = createClient(settings);
		for ( ;endMonthDate.isAfter(currentMonth)  || endMonthDate.isEqual(currentMonth); currentMonth = currentMonth.plusMonths(1)) {
			String currentMonthAsString = dateParser.print(currentMonth);
			logger.info("Extracting post ids for month: "+currentMonthAsString);
			List<PostInfo> posts = client.getPostIds(currentMonth);
			if (posts.size() == 0) {
				// No posts for month, continue to next month
				continue ;
			}
			logger.info("Filling queue with post ids for month");
			ArrayList<PostInfo> postIds = new ArrayList<PostInfo>();
			for (PostInfo postInfo : posts) {
				postIds.add(postInfo);
			}
			PostRange postRange = new PostRange();
			postRange.setPostIds(postIds);
			postRange.setPeriod(currentMonthAsString);
			String filename = "babyblog-"+postRange.getPeriod()+".pdf";
			postRange.setTargetFile(new File("target", filename));
			inputQueue.add(postRange);
		}
		logger.debug("Created "+inputQueue.size()+" slices");
	}

	protected WordpressClient createClient(Settings settings) throws NetworkException {
		return new WordpressClient(settings);
	}

	protected void handlePostErrors(Collection<PostError> errorQueue) {
		if (errorQueue.size() > 0) {
			logger.error("Errors occurred for following post ids (count="+errorQueue.size()+":");
			logger.error(errorQueue);
			Logger errorLogger = Logger.getLogger(PostError.class);
//			errorLogger.error("Id;Class;Message");
			for (PostError postError : errorQueue) {
				errorLogger.error(postError);
//				errorLogger.info(" ##############################");
//				errorLogger.info(postError.getPostContent());
//				errorLogger.info(postError.getStackTrace());
//				errorLogger.info(" ##############################");
			}
		}
	}
}
